﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Silverlight.Navigation.Mapping;

namespace Silverlight.Navigation.Controls
{
    public partial class MainMenu : UserControl
    {
        #region Events

        /// <summary>
        /// The navigate event.
        /// </summary>
        public event SubMenuNavigationEventHandler Navigate;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the MainMenu class.
        /// </summary>
        public MainMenu()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the MainMenu class.
        /// </summary>
        /// <param name="mainMenuItems">The items associated with the menu.</param>
        public MainMenu(MainMenuItemCollection mainMenuItems)
            : this()
        {
            if (mainMenuItems == null)
                throw new ArgumentNullException("mainMenuItems");

            this.MainMenuItems = mainMenuItems;
        }

        #endregion

        #region Properties

        /// <summary>
        /// The items associated with the menu.
        /// </summary>
        public MainMenuItemCollection MainMenuItems 
        {
            get
            {
                if (this.MainMenuItemsControl.ItemsSource != null)
                    return (MainMenuItemCollection)this.MainMenuItemsControl.ItemsSource;
                else
                    return null;
            }
            set
            {
                this.MainMenuItemsControl.ItemsSource = value;
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the click event of the button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// This displays the submenu for the menu item clicked.
        /// </remarks>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is Button)
            {
                Button sourceButton = (Button)e.OriginalSource;

                if (sourceButton.DataContext != null && sourceButton.DataContext is MainMenuItem)
                {
                    MainMenuItem mainMenuItem = (MainMenuItem)sourceButton.DataContext;
                    SubMenu subMenu = new SubMenu(mainMenuItem.SubMenus);
                    subMenu.Title = mainMenuItem.Title;
                    subMenu.Closed += new EventHandler(SubMenu_Closed);
                    subMenu.Show();
                }
            }
        }

        /// <summary>
        /// Handles the closed event for the sub menu.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SubMenu_Closed(object sender, EventArgs e)
        {
            if (sender is SubMenu)
            {
                SubMenu subMenu = (SubMenu)sender;

                if (subMenu.DialogResult.HasValue && subMenu.DialogResult.Value && subMenu.SelectedSubMenuItem != null)
                {
                    if (this.Navigate != null)
                        Navigate(this, new SubMenuNavigationEventArgs(subMenu.SelectedSubMenuItem));
                }
            }
        }

        #endregion
    }
}
